package com.klikmee.sms.core.base.communicator.mts;

import java.text.MessageFormat;

import org.apache.log4j.Logger;

import com.klikmee.sms.core.base.communicator.AbstractDeliveryReport2MessageResolver;
import com.klikmee.sms.core.base.communicator.DeliveryReport;
import com.klikmee.sms.core.base.exceptions.DeliveryReportNotResolvedException;
import com.klikmee.sms.core.commons.exceptions.ErrorInfo;
import com.klikmee.sms.core.model.OutgoingMessage;
/**
 * Delivery report resolver for the MTS communicator.
 * @author makcro
 *
 */
public class MtsDeliveryReport2MessageResolverImpl extends AbstractDeliveryReport2MessageResolver {

	/**
	 * Logger instance
	 */
	private Logger logger = Logger.getLogger(MtsDeliveryReport2MessageResolverImpl.class);
	
	/**
	 * Resolves delivery report using search criteria by 
	 * message id.
	 */
	@Override
	public OutgoingMessage resolve(DeliveryReport deliveryReport) throws DeliveryReportNotResolvedException {
		OutgoingMessage message = outgoingMessageDAO.find(deliveryReport.getMessageId());
		if(message == null){
			DeliveryReportNotResolvedException exception = new DeliveryReportNotResolvedException("Could not resolve delivery report for mts provider");
			ErrorInfo errorInfo = exception.addInfo();
			errorInfo.getParameters().put("MessageId", deliveryReport.getMessageId());
			
			throw exception;
		}
		logger.info(MessageFormat.format("Resolved delivery report to outgoing message id = [{0}]", message.getId()));
		return message;
	}

}
